package com.system.runtai.controller;


import com.system.common.result.Result;
import com.system.runtai.entity.Community;
import com.system.runtai.entity.ContactUs;
import com.system.runtai.entity.Qrcode;
import com.system.runtai.service.ContactUsService;
import com.system.runtai.service.QrcodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 联系我们 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "联系我们")
@RestController
@RequestMapping("/runtai/contact-us")
public class ContactUsController {

    @Autowired
    private ContactUsService contactUsService;


    @Autowired
    private QrcodeService qrcodeService;

    @ApiOperation(value = "联系我们修改")
    @PostMapping("/updateContactUs")
    public Result<ContactUs> updateContactUs(@RequestBody ContactUs contactUs){
        contactUs.setUpdateTime(LocalDateTime.now());


        List<Qrcode> qrcode = contactUs.getQrcode();
        List<Qrcode> list = qrcodeService.list();
        for (Qrcode qrcode1 : list) {

            qrcodeService.removeById(qrcode1.getId());
        }
        for (Qrcode qrcode1 : qrcode) {

            qrcodeService.save(qrcode1);
        }

        boolean b = contactUsService.updateById(contactUs);

        if (b){
            return Result.ok(contactUs).message("修改成功");
        }
        return Result.fail(contactUs).message("修改失败");
    }

    @ApiOperation(value = "查看联系我们")
    @PostMapping("/getContactUs")
    public Result<List<ContactUs>> getContactUs(){


        List<Qrcode> list1 = qrcodeService.list();
        List<ContactUs> list = contactUsService.list();

        for (ContactUs contactUs : list) {
            contactUs.setQrcode(list1);
        }
            return Result.ok(list);
    }
}

